package vn.cent.game.cardtype.client;

import vn.cent.game.cardtype.beans.GameBean;
import vn.cent.game.cardtype.main.CardTypeGameExtension;
import vn.cent.game.cardtype.main.RoomController;
import vn.cent.game.cardtype.utils.GameKind;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class RequestJoinCreatedRoom extends BaseClientRequestHandler{

	CardTypeGameExtension extension;
	GameKind gameKind;
	User sender;
	RoomController roomController;
	
	@Override
	public void handleClientRequest(User user, ISFSObject data) {
		// TODO Auto-generated method stub
		byte tempGameKind = data.getByte("GameKind");
		sender = user;
		
		int roomID = data.getInt("RoomID");
		
		SetupVariables();
		
		switch (tempGameKind) {
		case 1:
			gameKind = GameKind.BC;
			break;
		case 2:
			gameKind = GameKind.CA;
			break;
		default:
			break;
		}
		
		// Try to handle all game request 
		GameBean avaiableRoom = roomController.FindPrivateRoom(roomID);

		if (avaiableRoom != null)
		{
			JoinRoom(avaiableRoom);
		}
		else
		{
			//CreateRoomBaseGameKind();
			// Show join room errors
		}
	}
	
	private void SetupVariables()
	{
		extension = (CardTypeGameExtension) getParentExtension();
		roomController = new RoomController(this.getApi(), extension, sender);
	}

	private void JoinRoom(GameBean room)
	{
		roomController.JoinPublicRoom(room);
	}
	
	/* Check later 
	private void CreateRoomBaseGameKind()
	{
		roomController.CreatePublicBCRoom();
	}
	*/

}
